<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

class KBClientController
{
	var $query 		= array('view'        =>'View',
                            'category_id' =>'CategoryID',
				  	        'entry_id'    =>'EntryID',
					        'msg'  		  =>'Msg'
				   		    );
	
	var $request;
	
	var $working_dir;
	var $working_path;	
	var $mod_rewrite = false;
	var $kb_path;
	var $client_path;
	var $setting = array();
	
	var $extra_params = array();
	var $dirs = array();
	var $arg_separator = '&amp;';
    var $view_format_value = 'default';
	
	
	function KBClientController() {
		$this->setUrlVars();
	}
	
	
	function setDirVars(&$settings) {
	
		$client_dir = APP_CLIENT_DIR . 'client/inc/';
	
		$this->kb_path     = APP_CLIENT_PATH;
		$this->kb_dir      = APP_CLIENT_DIR;
		$this->client_path = APP_CLIENT_PATH . 'client/';
		$this->skin_dir    = APP_CLIENT_DIR . 'client/skin/';
		$this->skin_path   = APP_CLIENT_PATH . 'client/skin/';
		
		$this->common_dir          = $client_dir;
		$this->default_working_dir = $client_dir . 'default/';
        switch ($settings['view_format']) {
            case '1':
            $this->view_format_value         = 'default';
            break;
            case '2':
            $this->view_format_value         = 'faq';
            break;
            case '3':
            $this->view_format_value         = 'faq';
            break;
            case '4':
            $this->view_format_value         = 'left';
            break;
        }
        $this->working_dir         = $client_dir . $this->view_format_value . '/';
		$this->setting = &$settings;		
	}
	
	
	function setUrlVars() {
		
		//$this->category_key = $this->getRequestVar('category_id');		
		$this->category_id = (int) $this->getRequestVar('category_id');

		//$this->entry_key = $this->getRequestKey('entry_id');
		$this->entry_id = (int) $this->getRequestVar('entry_id');
		
		$this->view_key = $this->getRequestKey('view');
		$view_id = $this->getRequestVar('view');
		$this->view_id = ($view_id) ? $view_id : 'index';
		
		//$this->msg_key = $this->getRequestKey('msg');
		$this->msg_id = $this->getRequestVar('msg');		
	}
	
	
	function setSettings(&$settings) {
		$this->setting = &$settings;
	}
	
	
	function getSetting($setting_key) {
		return @$this->setting[$setting_key];
	}
	
	
	function setModRewrite($var) {
		$this->mod_rewrite = $var;
	}
	
	
	function getRequestVar($var) {
		return @$_GET[$this->query[$var]];
	}
	
	
	function getRequestKey($key) {
		return @$this->query[$key];
	}
	
	
	function go($view = false, $category_id = false, $entry_id = false, $msg_key = false) {
		$this->arg_separator = '&';
		ini_set('arg_separator.output', $this->arg_separator);
		$url = $this->getLink($view, $category_id, $entry_id, $msg_key);
		header("Location: " . $url);
		exit();
	}	
	
	
	function getLink($view = false, $category_id = false, $entry_id = false, $msg_key = false) {
		if($view == 'all') {
			return $this->_getLink($this->view_id, 
			                       $this->category_id,
								   $this->entry_id, 
								   $this->msg_id); 	
		} else {
			return $this->_getLink($view, $category_id, $entry_id, $msg_key);
		}
	}
		
	
	function _getLink($view = 'index', $category_id = false, $entry_id = false, $msg_key = false) {
		
		$link = array();
		$category_id = ($view == 'index' || $view == 'file') ? $category_id : false;
		$view = ($view == 'index') ? false : $view;
		
		if(!$this->mod_rewrite) {
		
			if($view)        { $link[1] = sprintf('%s=%s', $this->getRequestKey('view'), $view); }
			if($category_id) { $link[2] = sprintf('%s=%d', $this->getRequestKey('category_id'), $category_id); }
			if($entry_id)    { $link[3] = sprintf('%s=%d', $this->getRequestKey('entry_id'), $entry_id); }
			if($msg_key)     { $link[4] = sprintf('%s=%s', $this->getRequestKey('msg'), $msg_key); }
			if($this->extra_params) { $link[5] = http_build_query($this->extra_params); }
			
			$link = (!$link) ? $this->kb_path : $this->kb_path . '?' . implode($this->arg_separator, $link);
		
		} else {
		
			if($view)        { $link[1] = sprintf('%s', $view); }
			if($category_id) { $link[2] = sprintf('%d', $category_id); }
			if($entry_id)    { $link[3] = sprintf('%d', $entry_id); }
			if($msg_key)     { $link[4] = sprintf('%s', $msg_key); }
			
			$link = (@!$link) ? $this->kb_path : $this->kb_path . implode('/', $link) . '/';
		}
		
		return $link;
	}
	
	
	function getView($view_id = false) {
		
		if(!$view_id){
			$view_id = $this->view_id;
		}
		
		$class = 'KBClientView_'. $view_id;
		
		$files = array();
		$files[]  = $this->working_dir . 'view/' . $class . '.php';
		$files[]  = $this->default_working_dir . 'view/' . $class . '.php';
		
		foreach($files as $file) {
			if(file_exists($file)) {
				require_once $file;
				return new $class;
			}			
		}
		
		
		$this->go();
		//require_once $this->default_working_dir . 'view/KBClientView_404.php';
		//return new KBClientView_404;
 	}
	
	
/*
	function &getAction($view_id = false) {
		
		if(!$view_id && !$this->getRequestVar('view')) {
			$view_id = 'index';
		} elseif(!$view_id){
			$view_id = $this->getRequestVar('view');
		}
		
		$class = 'KBClientAction_'. $view_id;
		require_once $this->working_dir . 'action/' . $class . '.php';
		
		return new $class;
 	}
*/
}
?>